//
//  RankingListController.swift
//  FastFinger
//
//  Created by mugua on 2018/6/26.
//  Copyright © 2018年 mugua. All rights reserved.
//

import UIKit

class RankingListController: UIViewController {
    
    var dataSouce: [String] = []
    
    @IBOutlet weak var tableView: UITableView! {
        didSet {
            tableView.tableFooterView = UIView(frame: .zero)
            tableView.rowHeight = 60
            tableView.register(UINib.init(nibName: "RankListCell", bundle: nil), forCellReuseIdentifier: "RankListCell")
            tableView.delegate = self
            tableView.dataSource = self
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(false, animated: animated)
        
        let json = Storage.load(key: AppDefaultKey.scoreHistorKey) as? [String]
        SVProgressHUD.show()
        
        SVProgressHUD.dismiss(withDelay: 1) {
            if let j = json {
                self.dataSouce = j
            }
            self.tableView.reloadSections(IndexSet(integer: 0), with: .fade)
        }
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        title = "排行榜"
        
        navigationController?.navigationBar.barTintColor = #colorLiteral(red: 0.2745098174, green: 0.4862745106, blue: 0.1411764771, alpha: 1)
        navigationController?.navigationBar.tintColor = .white
        navigationController?.navigationBar.isTranslucent = true
        navigationController?.setNavigationBarHidden(false, animated: true)
    }
}

extension RankingListController: UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSouce.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "RankListCell", for: indexPath) as! RankListCell
        let name = dataSouce[indexPath.row]
        
        cell.nameLabel.text = name
        cell.rankLabel.text = (indexPath.row + 1).description
        
        return cell
    }
}

extension RankingListController: UITableViewDelegate {
    
}
